using System;
using System.Collections.Generic;
using System.Text;
using IGGCore;

namespace IGGConsole
{
    public class ModeStatsFormatter : IModeFormatter
    {
        private int m_statsNum = 1;

        public ModeStatsFormatter()
        {
        }

        public ModeStatsFormatter(int num)
        {
            m_statsNum = num;
        }



        /// <summary>
        /// Find a particular stat amongst the stats
        /// </summary>
        private StatItem GetStat(string subject, List<StatItem> items)
        {
            StatItem result = null;
            foreach (StatItem item in items)
            {
                if (item.Subject == subject)
                {
                    result = item;
                    break;
                }
            }
            return result;
        }

        /// <summary>
        /// Format a stat for the 2 leagues
        /// </summary>
        private void FormatStat(string subject, List<StatItem> NLStats, List<StatItem> ALStats)
        {
            StatItem itemNL = GetStat(subject, NLStats);
            StatItem itemAL = GetStat(subject, ALStats);
            Console.WriteLine(" " + itemNL.Subject);
            for (int j = 0; j < 6; j++)
            {
                if (itemNL.Names.Count > j)
                {
                    Console.Write(" ");
                    Console.Write(ToolBox.GetFormattedStringPadRight(
                        itemNL.Names[j] + ", " +
                        itemNL.Teams[j], 25));
                    Console.Write(ToolBox.GetFormattedStringPadRight(itemNL.Values[j], 10));
                    if (itemAL!=null && itemAL.Names.Count > j)
                    {
                        Console.Write(ToolBox.GetFormattedStringPadRight(
                            itemAL.Names[j] + ", " +
                            itemAL.Teams[j], 25));
                        Console.Write(itemAL.Values[j]);
                    }
                    Console.WriteLine();
                }
                else if (itemAL != null && itemAL.Names.Count > j)
                {
                    Console.Write(ToolBox.GetFormattedStringPadRight("", 36));
                    Console.Write(ToolBox.GetFormattedStringPadRight(
                            itemAL.Names[j] + ", " +
                            itemAL.Teams[j], 25));
                    Console.Write(itemAL.Values[j]);
                    Console.WriteLine();
                }
                else
                {
                    //Console.WriteLine();
                }
            }
            Console.WriteLine();
        }

        public void FormatMode(Scoreboard board)
        {
            if (board.CommonStats == null)
            {
                Console.WriteLine();
                switch (m_statsNum)
                {
                    case 1:
                        FormatStat("Batting Average", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("On-Base%", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("Home Runs", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("Slugging %", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("On-Base Plus Slugging", board.StatsNLBatting, board.StatsALBatting);
                        break;
                    case 2:
                        FormatStat("Hits", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("Bases on Balls", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("Singles", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("Doubles", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("Triples", board.StatsNLBatting, board.StatsALBatting);
                        break;
                    case 3:
                        FormatStat("Stolen Bases", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("Caught Stealing", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("Runs Scored", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("Runs Batted In", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("Adjusted OPS+", board.StatsNLBatting, board.StatsALBatting);
                        break;
                    case 4:
                        //FormatStat("WAR Position Players", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("Strikeouts", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("Intentional Bases on Balls", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("Extra Base Hits", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("Sacrifice Flies", board.StatsNLBatting, board.StatsALBatting);
                        break;
                    case 5:
                        FormatStat("At Bats", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("Double Plays Grounded Into", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("Plate Appearances", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("Games Played", board.StatsNLBatting, board.StatsALBatting);
                        FormatStat("Hit By Pitch", board.StatsNLBatting, board.StatsALBatting);
                        break;
                    case 6:
                        FormatStat("Earned Run Average", board.StatsNLPitching, board.StatsALPitching);
                        FormatStat("Wins", board.StatsNLPitching, board.StatsALPitching);
                        FormatStat("Walks & Hits per IP", board.StatsNLPitching, board.StatsALPitching);
                        FormatStat("Saves", board.StatsNLPitching, board.StatsALPitching);
                        FormatStat("Strikeouts", board.StatsNLPitching, board.StatsALPitching);
                        break;
                    case 7:
                        //FormatStat("WAR for Pitchers", board.StatsNLPitching, board.StatsALPitching);
                        FormatStat("Win-Loss %", board.StatsNLPitching, board.StatsALPitching);
                        FormatStat("Complete Games", board.StatsNLPitching, board.StatsALPitching);
                        FormatStat("Shutouts", board.StatsNLPitching, board.StatsALPitching);
                        FormatStat("Wild Pitches", board.StatsNLPitching, board.StatsALPitching);
                        break;
                    case 8:
                        FormatStat("Losses", board.StatsNLPitching, board.StatsALPitching);
                        FormatStat("Innings Pitched", board.StatsNLPitching, board.StatsALPitching);
                        FormatStat("Games Started", board.StatsNLPitching, board.StatsALPitching);
                        FormatStat("Hit By Pitch", board.StatsNLPitching, board.StatsALPitching);
                        FormatStat("Adjusted ERA+", board.StatsNLPitching, board.StatsALPitching);
                        break;
                    case 9:
                        FormatStat("Hits per 9 IP", board.StatsNLPitching, board.StatsALPitching);
                        FormatStat("Bases On Balls per 9 IP", board.StatsNLPitching, board.StatsALPitching);
                        FormatStat("Strikeouts per 9 IP", board.StatsNLPitching, board.StatsALPitching);
                        FormatStat("Strikeouts / Base On Balls", board.StatsNLPitching, board.StatsALPitching);
                        FormatStat("Earned Runs", board.StatsNLPitching, board.StatsALPitching);
                        break;
                    case 0:
                        FormatStat("Putouts", board.StatsNLFielding, board.StatsALFielding);
                        FormatStat("Assists", board.StatsNLFielding, board.StatsALFielding);
                        FormatStat("Errors Committed", board.StatsNLFielding, board.StatsALFielding);
                        //FormatStat("Stolen Bases", board.StatsNLFielding, board.StatsALFielding);
                        //FormatStat("Caught Stealing %", board.StatsNLFielding, board.StatsALFielding);
                        break;
                }
            }
            else
            {
                Console.WriteLine(" Spring Training Stats");
                Console.WriteLine();
                for (int dd = 0; dd < board.CommonStats.Count; dd+=2 )
                {
                    Console.WriteLine(" " + board.CommonStats[dd].Subject.ToUpper().Substring(3));
                    for (int i = 0; i < 6; i++)
                    {
                        if (board.CommonStats[dd].Names.Count > i)
                        {
                            Console.Write(" ");
                            Console.Write(ToolBox.GetFormattedStringPadRight(
                                board.CommonStats[dd].Names[i] + ", " +
                                board.CommonStats[dd].Teams[i], 25));
                            Console.Write(ToolBox.GetFormattedStringPadRight(board.CommonStats[dd].Values[i], 10));
                            if (board.CommonStats[dd + 1] != null && board.CommonStats[dd + 1].Names.Count > i)
                            {
                                Console.Write(ToolBox.GetFormattedStringPadRight(
                                    board.CommonStats[dd + 1].Names[i] + ", " +
                                    board.CommonStats[dd + 1].Teams[i], 25));
                                Console.Write(board.CommonStats[dd + 1].Values[i]);
                            }
                            Console.WriteLine();
                        }
                        else if (board.CommonStats[dd + 1] != null && board.CommonStats[dd + 1].Names.Count > i)
                        {
                            Console.Write(ToolBox.GetFormattedStringPadRight("", 36));
                            Console.Write(ToolBox.GetFormattedStringPadRight(
                                    board.CommonStats[dd + 1].Names[i] + ", " +
                                    board.CommonStats[dd + 1].Teams[i], 25));
                            Console.Write(board.CommonStats[dd + 1].Values[i]);
                            Console.WriteLine();
                        }
                        else
                        {
                            //Console.WriteLine();
                        }
                    }
                    Console.WriteLine();
                }
            }

        }
    }
}
